package org.apache.ant.extensions.notify;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;

class TaskTriggerHandle implements FileEventsHandler {

	private Project project;
	private String targetTask;

	public TaskTriggerHandle(Project project, String targetTask) {
		this.project = project;
		this.targetTask = targetTask;
	}

	@Override
	public void fileCreated(String absFileName) {
		project.log("File created " + absFileName , Project.MSG_DEBUG);
		runTask();
	}

	@Override
	public void fileDeleted(String absFileName) {
		project.log("File deleted" + absFileName , Project.MSG_DEBUG);
		runTask();
	}

	@Override
	public void fileModified(String absFileName) {
		project.log("File modified" + absFileName , Project.MSG_DEBUG);
		runTask();
	}
	
	
	private void runTask(){
		try {
			project.executeTarget(targetTask);
		} catch (BuildException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void log(String msg) {
		project.log( msg );
	}

	
	
}
